import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose

# 读取CSV文件
file_path = 'Demodata.csv'
df = pd.read_csv(file_path)

# 将Date列转换为日期时间格式
df['Date'] = pd.to_datetime(df['Date'])

# 设置Date列为索引
df.set_index('Date', inplace=True)

# 提取h1列的数据
h1_column = df['h1']

# 1. 数据可视化
plt.figure(figsize=(14, 8))

# 绘制原始数据
plt.subplot(2, 1, 1)
plt.plot(h1_column, label='h1', color='blue', marker='o')
plt.title('Trend of h1 Column')
plt.xlabel('Date')
plt.ylabel('Values')
plt.legend()
plt.grid(True)

# 2. 移动平均线
rolling_mean = h1_column.rolling(window=12).mean()
plt.plot(rolling_mean, label='12-Month Rolling Mean', color='red')
plt.legend()

# 3. 统计分析
mean_value = h1_column.mean()
median_value = h1_column.median()
std_dev = h1_column.std()

print(f"Mean: {mean_value}")
print(f"Median: {median_value}")
print(f"Standard Deviation: {std_dev}")

# 4. 季节性和周期性分析
decomposition = seasonal_decompose(h1_column, model='additive', period=12)
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

# 绘制分解结果
plt.subplot(2, 2, 3)
plt.plot(trend, label='Trend', color='green')
plt.title('Trend Component')
plt.xlabel('Date')
plt.ylabel('Values')
plt.legend()
plt.grid(True)

plt.subplot(2, 2, 4)
plt.plot(seasonal, label='Seasonal', color='orange')
plt.title('Seasonal Component')
plt.xlabel('Date')
plt.ylabel('Values')
plt.legend()
plt.grid(True)

plt.tight_layout()
plt.show()